Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Text
Imports System.Data.Odbc
Public Class FrmRcdPriorTreatment
    Public GV_ISEDIT As Boolean = False
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Dim vCurrentStatus As String = ""
        Dim SqlConnection2 As New SqlConnection()
        SqlConnection2 = New SqlConnection(GV_CONNETION)
        If SqlConnection2.State = ConnectionState.Closed Then SqlConnection2.Open()
        Dim sqlCmd2 = New SqlCommand("select status from event where Event_ID = '" & GV_SELECTED_EVENTID & "' and status <> 'D'", SqlConnection2)
        Try
            Dim objReader As SqlDataReader = sqlCmd2.ExecuteReader
            Do While objReader.Read
                vCurrentStatus = IIf(IsDBNull(objReader("status")), "", objReader("status"))
            Loop

        Catch ex As Exception
            MsgBox(ex.Source)
        End Try

        sqlCmd2.Cancel()
        SqlConnection2.Close()
        sqlCmd2 = Nothing
        SqlConnection2.Close()

        If (vCurrentStatus = "P") Then
            'set save button to allow enable or disable in the presentation page
            FrmPresentation.btnSave.Enabled = False
        Else
            'set save button to allow enable or disable in the presentation page
            FrmPresentation.btnSave.Enabled = True
        End If
        Close()
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        GV_ISEDIT = False
        Panel2.Enabled = True
        btnEdit.Visible = False
        btnDel.Visible = False
        BtnAdd.Visible = False
        btnSave.Visible = True
        btnCancel.Visible = False 'modified
        btnSave.Enabled = True
        ClearText()
    End Sub
    Private Sub ClearText()
        txtTxDetail.Text = ""
        TxtSiteRelapse.Text = ""
        TxtTumourSize.Text = ""
        DOB.Value = "01/01/1900"
        cbxBiopsyDone.SelectedIndex = -1
    End Sub

    Private Sub FrmRcdPriorTreatment_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Panel2.Enabled = False
        LoadBiopsyDone()
        LoadPatientBiodata()

        Panel1.Enabled = GV_FRMEVENT_ALLOW_EDIT
        If List_1stRecurrence() = False Then

            btnEdit_Click(sender, e)
        Else
            BtnAdd_Click(sender, e)
        End If


        If Approved() = True Then
            'btnAdd.Enabled = False
            'btnEdit.Enabled = False
            'btnDel.Enabled = False
            'Panel1.Enabled = False
            'Panel2.Enabled = False
            btnEdit.Enabled = False
            btnSave.Enabled = False
            BtnLock.Text = "Unlock"
            btnDel.Enabled = False
        Else
            btnSave.Enabled = True
        End If

        btnCancel.Visible = False 'added/modified


        'validation added by cherwei on page load event to check for status 'P' = disable save, status 'A' = enable save
        Dim vStatus As String = ""
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        'Dim sqlCmd = New SqlCommand("select status from PRIORTX where PRIORTX_ID = '" & GV_SELECTED_PRIOR_TREATMENT_ID & "' and status <> 'D'", SqlConnection1)
        Dim sqlCmd = New SqlCommand("select status from EVENT where EVENT_ID = '" & GV_SELECTED_EVENTID & "' and status <> 'D'", SqlConnection1)

        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                vStatus = IIf(IsDBNull(objReader("Status")), "", objReader("Status"))
            Loop

        Catch ex As Exception
            MsgBox(ex.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()

        'If vStatus = "P" Then
        'btnSave.Enabled = False
        'ElseIf vStatus = "" Then
        'btnSave.Enabled = False
        'Else

        'btnSave.Enabled = True
        'End If
        If (vStatus = "P" And List_1stRecurrence() = True) Then
            btnSave.Enabled = False
        End If

    End Sub
    Private Sub LoadPatientBiodata()
        Dim vGender As String = ""
        Dim vAge As String = ""

        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select ptNRIC,PtBirthDate,PtName,PtGender,PtAge from patients where pt_id1 = '" & GV_CUR_PATID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                vGender = IIf(IsDBNull(objReader("PtGender")), "", objReader("PtGender"))
                vAge = IIf(IsDBNull(objReader("PtAge")), "", objReader("PtAge"))
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        LblName.Text = GV_CUR_PATNAME
        LblNRIC.Text = GV_CUR_PATNRIC
        LblAge.Text = vAge & " years /  " & vGender

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Function Approved() As Boolean
        Dim vStatus As String = ""
        Dim vLockBy As String = ""
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        'Dim sqlCmd = New SqlCommand("select * from PriorTx where PriorTx_ID = '" & GV_SELECTED_PRIOR_TREATMENT_ID & "'", SqlConnection1)
        Dim sqlCmd = New SqlCommand("select top 1 e.status as status, e.lockedby  from PriorTx p,event e where p.event_id = e.event_id and p.PriorTx_ID = '" & GV_SELECTED_PRIOR_TREATMENT_ID & "'", SqlConnection1)

        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                vStatus = IIf(IsDBNull(objReader("Status")), "", objReader("Status"))
                vLockBy = IIf(IsDBNull(objReader("LockedBy")), "", objReader("LockedBy"))
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
        'If vStatus <> "" Then
        'Approved = True
        'Else
        'Approved = False
        'End If
        If vLockBy <> "" Then
            Approved = True
        Else
            Approved = False
        End If
    End Function
    Private Sub LoadBiopsyDone()


        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 112", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("Code_Desc", GetType(String)))
            list.Columns.Add(New DataColumn("Code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("Code_Desc")), "", objReader("Code_Desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("Code")), "", objReader("Code"))
                i = i + 1
            Loop
            Me.cbxBiopsyDone.DataSource = list
            Me.cbxBiopsyDone.DisplayMember = "Code_Desc"
            Me.cbxBiopsyDone.ValueMember = "Code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    'validation added by cherwei
    Private Function ValidateRcdPriorTreatment() As Boolean
        Dim flag As String
        'Perform necessary validation for the required fields
        flag = 0

        If (LTrim(RTrim(TxtTumourSize.Text)) = "") Or (LTrim(RTrim(TxtTumourSize.Text)) = "") Or (LTrim(RTrim(TxtTumourSize.Text)) = "") Then
            'validation to allow null
        Else

            If Not IsNumeric(TxtTumourSize.Text) Or TxtTumourSize.Text.IndexOf(",") <> -1 Then
                'Me.ErrorProvider1.SetError(Me.txtTumourSize, "Please Enter numeric values for tumor size")
                MsgBox("Please enter numeric values for tumor size")
                'Throw New Exception("Please enter a numeric value")
                flag = 1
                TxtTumourSize.Focus()
                'Else if isnumeric(
            End If
        End If

        If flag = 0 Then
            ValidateRcdPriorTreatment = True 'validation pass
        Else
            ValidateRcdPriorTreatment = False 'validation fail
        End If
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If ValidateRcdPriorTreatment() = True Then
            SaveReccurent()
            Panel2.Enabled = False
            Close()
        Else

        End If
    End Sub
    Private Sub SaveReccurent()
        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spInsertPriorTx", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()

        myCommand.Parameters.Add("@PriorTx_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Event_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Pt_id1", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Histo_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Prior_Tx_Details", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Sites_Relapses", SqlDbType.VarChar)
        myCommand.Parameters.Add("@PriorTx_clin_tum_size", SqlDbType.VarChar)
        myCommand.Parameters.Add("@date_biopsy", SqlDbType.DateTime)
        myCommand.Parameters.Add("@biopsy_type", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)

        If GV_ISEDIT = True Then
            myCommand.Parameters("@PriorTx_ID").Value = GV_SELECTED_PRIOR_TREATMENT_ID
        Else
            myCommand.Parameters("@PriorTx_ID").Value = "0"
        End If
        myCommand.Parameters("@Event_ID").Value = GV_SELECTED_EVENTID
        myCommand.Parameters("@Pt_id1").Value = GV_CUR_PATID
        myCommand.Parameters("@Histo_ID").Value = GV_SELECTED_HISTOLOGYID
        myCommand.Parameters("@Prior_Tx_Details").Value = Trim(txtTxDetail.Text)
        myCommand.Parameters("@Sites_Relapses").Value = Trim(TxtSiteRelapse.Text)
        myCommand.Parameters("@PriorTx_clin_tum_size").Value = Trim(TxtTumourSize.Text)
        myCommand.Parameters("@date_biopsy").Value = DOB.Value
        myCommand.Parameters("@biopsy_type").Value = Trim(cbxBiopsyDone.Text)
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If GV_SELECTED_EVENTID <> "" Then
            Dim response As Integer

            response = MsgBox("Are you sure want to delete this record?", MsgBoxStyle.YesNo, "Delete")
            If response = 7 Then
                Exit Sub
            End If

            Dim myCommand As SqlCommand
            Dim SqlConn As New SqlConnection()
            SqlConn = New SqlConnection(GV_CONNETION)
            myCommand = New SqlCommand("spDelPriorTx", SqlConn)
            myCommand.CommandType = CommandType.StoredProcedure
            SqlConn.Open()

            myCommand.Parameters.Add("@PriorTx_ID", SqlDbType.VarChar)
            myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
            myCommand.Parameters("@PriorTx_ID").Value = GV_SELECTED_PRIOR_TREATMENT_ID
            myCommand.Parameters("@EnteredBy").Value = GV_USER
            myCommand.ExecuteNonQuery()

            myCommand.Cancel()
            SqlConn.Close()
            myCommand = Nothing
            SqlConn = Nothing
            Close()
        End If
    End Sub
    Function List_1stRecurrence() As Boolean
        Dim vRec As String
        vRec = ""
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        'Dim sqlCmd = New SqlCommand("select top 1 PriorTx_ID from PriorTx where status <> 'D' and Histo_ID  = '" & GV_SELECTED_HISTOLOGYID & "'", SqlConnection1)
        Dim sqlCmd = New SqlCommand("select top 1 Priortx_id from priortx where status <> 'D' and Event_id = '" & GV_SELECTED_EVENTID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                vRec = IIf(IsDBNull(objReader("PriorTx_ID")), "", objReader("PriorTx_ID"))
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
        If vRec <> "" Then
            ListRecurrence(vRec)
            GV_SELECTED_PRIOR_TREATMENT_ID = vRec
            List_1stRecurrence = False
            Exit Function
        End If
        List_1stRecurrence = True
    End Function
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        If GV_ISEDIT = False Then ClearText() Else ListRecurrence(GV_SELECTED_PRIOR_TREATMENT_ID)
        Panel2.Enabled = False
        BtnAdd.Visible = True
        btnEdit.Visible = True
        btnDel.Visible = True
        btnSave.Visible = False
        btnCancel.Visible = False
    End Sub
    Private Sub ListRecurrence(ByVal vID)
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select * from PriorTx where PriorTx_ID = '" & vID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                txtTxDetail.Text = IIf(IsDBNull(objReader("Prior_Tx_Details")), "", objReader("Prior_Tx_Details"))
                TxtSiteRelapse.Text = IIf(IsDBNull(objReader("Sites_Relapses")), "", objReader("Sites_Relapses"))
                TxtTumourSize.Text = IIf(IsDBNull(objReader("PriorTx_clin_tum_size")), "", objReader("PriorTx_clin_tum_size"))
                DOB.Value = IIf(IsDBNull(objReader("date_biopsy")), "01/01/1900", objReader("date_biopsy"))
                'Dim currenttext As String = IIf(IsDBNull(objReader("biopsy_type")), "", objReader("biopsy_type"))
                Dim currenttext As String = IIf(IsDBNull(objReader("biopsy_type")), "", objReader("biopsy_type"))
                cbxBiopsyDone.Text = currenttext
                'cbxBiopsyDone.SelectedText = IIf(IsDBNull(objReader("biopsy_type")), "", objReader("biopsy_type"))
                Lbl_EnBy.Text = "Entered By:" & vbCrLf & IIf(IsDBNull(objReader("EnteredBy")), "", objReader("EnteredBy"))
                Lbl_UpdBy.Text = "Updated By:" & vbCrLf & IIf(IsDBNull(objReader("UpdatedBy")), "", objReader("UpdatedBy"))
                Lbl_LockBy.Text = "Locked By:" & vbCrLf & IIf(IsDBNull(objReader("LockedBy")), "", objReader("LockedBy"))

            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If GV_SELECTED_EVENTID <> "" Then
            GV_ISEDIT = True
            Panel2.Enabled = True
            btnEdit.Visible = False
            btnDel.Visible = False
            BtnAdd.Visible = False
            btnSave.Visible = True
            btnCancel.Visible = True
        End If
    End Sub

    Private Sub BtnLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLock.Click
        If BtnLock.Text = "Lock" Then
            If GV_FRMEVENT_ALLOW_APPROVE = False Then
                MsgBox("Permission denied.", MsgBoxStyle.Information)
                Exit Sub
            End If
        End If

        If BtnLock.Text = "UnLock" Then
            If GV_FRMEVENT_ALLOW_APPROVE = False Then
                MsgBox("Permission denied.", MsgBoxStyle.Information)
                Exit Sub
            End If
        End If

        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("sp_LockUnlockPriorTx", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()
        myCommand.Parameters.Add("@PriorTx_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
        myCommand.Parameters.Add("@LockFlag", SqlDbType.VarChar)

        myCommand.Parameters("@PriorTx_ID").Value = GV_SELECTED_PRIOR_TREATMENT_ID
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.Parameters("@LockFlag").Value = Trim(BtnLock.Text)
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing

        If BtnLock.Text = "Lock" Then
            BtnLock.Text = "UnLock"
            btnEdit.Enabled = False
            btnSave.Enabled = False
            btnDel.Enabled = False
            Lbl_LockBy.Text = "Locked By:" & vbCrLf & GV_USERNAME
        Else
            BtnLock.Text = "Lock"
            btnEdit.Enabled = GV_FRMEVENT_ALLOW_EDIT
            btnSave.Enabled = GV_FRMEVENT_ALLOW_EDIT
            btnDel.Enabled = GV_FRMEVENT_ALLOW_EDIT
            Lbl_LockBy.Text = "Locked By:"
        End If
    End Sub

    Private Sub txtTxDetail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTxDetail.TextChanged
        Label8.Text = txtTxDetail.Text.Length
        Label8.Text = Label8.Text + "/500"
    End Sub

    Private Sub TxtSiteRelapse_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtSiteRelapse.TextChanged
        Label9.Text = TxtSiteRelapse.Text.Length
        Label9.Text = Label9.Text + "/500"
    End Sub
End Class